<?php

class Application_Model_Product extends Zend_Db_Table_Abstract
{
	protected $_name = 'product';
	protected $_primary = 'product_id';

	//get all product in database
	public function Get_All_Product(){
		$select = $this->select()
		               ->order("product_dateupdate DESC");
		return $select;
	}

	//get product with manufacture_id
	public function Get_Product_by_ManufactureID($manufacture_id){
		$select = $this->select()->from($this->_name)
						->setIntegrityCheck(false)
						->join("manufacture", "manufacture.manufacture_id = product.manufacture_id")
						->where("product.manufacture_id = ?", $manufacture_id);
		return $select;
	}
	
	//get product by product_id
	public function Get_Product_by_ID($product_id){
	    $select = $this->select()
	                    ->setIntegrityCheck(false)
	                    ->from($this->_name)
	                    ->where("product_id = ?", $product_id)
	                    ->join('manufacture', 'manufacture.manufacture_id = product.manufacture_id');
	    return $this->fetchRow($select);
	}
	
	//search product with keyword, manufacture or price
	public function Search_Product($keyword, $manufacture, $pricefrom, $priceto){
		try {
		    $select = $this->select()
    	                    ->from($this->_name)
    	                    ->setIntegrityCheck(false)
    	                    ->join("manufacture", "manufacture.manufacture_id = product.manufacture_id")
    	                    ->order('product_name ASC');
		    if($keyword != 'Từ khóa tìm kiếm'){
		        $select->where("product_name like ?", "%".$keyword."%");
		    }
		    if($manufacture != 0){
		        $select->where('product.manufacture_id = ?', $manufacture);
		    }
		    if($pricefrom != 0){
		        $select->where('product_price >= ?', $pricefrom);
		    }
		    if($priceto != 0){
		        $select->where('product_price <= ?', $priceto);
		    }
		    return $select;
		}
		catch (Zend_Exception $ex){
		    echo 'Error: ' .$ex->getMessage();
		}
	}
	
	//get 6 product newest
	public function Get_3_Product_Newest(){
	    try {
	        $select = $this->select()
	                        ->order("product_dateupdate DESC")
	                        ->limit(6);
	        return $this->fetchAll($select);
	    }
	    catch (Zend_Exception $ex){
	        echo 'Error: '. $ex->getMessage();
	    }
	}

}

